package com.weichen.studentgameexchange.comment.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.weichen.studentgameexchange.comment.entity.CommentLike;
import com.weichen.studentgameexchange.comment.dao.CommentLikeMapper;
import com.weichen.studentgameexchange.comment.entity.vo.AddLikeVo;
import com.weichen.studentgameexchange.comment.service.ICommentLikeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weichen.studentgameexchange.common.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 惟尘
 * @since 2025-02-05
 */
@Service
public class CommentLikeServiceImpl extends ServiceImpl<CommentLikeMapper, CommentLike> implements ICommentLikeService {

    @Autowired
    CommentLikeMapper commentLikeMapper;


    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<CommentLike> adminPage(Integer page, Integer size) {
        Page<CommentLike> commentLikePage = new Page<>(page, size);
        LambdaQueryWrapper<CommentLike> commentLikeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLikeLambdaQueryWrapper.orderByDesc(CommentLike::getCommentId);
        return commentLikeMapper.selectPage(commentLikePage, commentLikeLambdaQueryWrapper);
    }

    @Override
    public Result userAdd(AddLikeVo addLikeVo) {
        CommentLike like = BeanUtil.copyProperties(addLikeVo, CommentLike.class);
        LambdaQueryWrapper<CommentLike> commentLikeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLikeLambdaQueryWrapper
                .eq(CommentLike::getCommentId,like.getCommentId())
                .eq(CommentLike::getUserId,like.getUserId());
        CommentLike one = commentLikeMapper.selectOne(commentLikeLambdaQueryWrapper);
        if (one != null){
            commentLikeMapper.deleteById(one);
            return Result.success("取消点赞成功");
        }
        commentLikeMapper.insert(like);
        return Result.success("点赞成功");
    }

    @Override
    public Result userDelete(AddLikeVo addLikeVo) {
        Db.lambdaUpdate(CommentLike.class)
                .eq(CommentLike::getCommentId,addLikeVo.getCommentId())
                .eq(CommentLike::getUserId,addLikeVo.getUserId())
                .remove();
        return Result.success("取消点赞成功");
    }

    @Override
    public Result userSelect(Long commentId) {
        LambdaQueryWrapper<CommentLike> commentLikeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        commentLikeLambdaQueryWrapper
                .eq(CommentLike::getCommentId,commentId);
        List<CommentLike> commentLikes = commentLikeMapper.selectList(commentLikeLambdaQueryWrapper);
        return Result.success(commentLikes);
    }
}
